package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.AdminDTO;
import com.woniu.entity.QueryAdminDTO;
import com.woniu.pojo.Admin;
import com.woniu.service.IAdminService;
import com.woniu.util.PageData;
import com.woniu.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags = "admin模块")
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    IAdminService adminService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public Result toLogin(@RequestBody AdminDTO adminDTO) {
        log.debug("进行了登录方法，用户名为:{},密码为:{}", adminDTO.getUsername(), adminDTO.getPassword());
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUserName,adminDTO.getUsername());
        queryWrapper.eq(Admin::getPassword,adminDTO.getPassword());
        Admin admin = adminService.getOne(queryWrapper);
        Map<String, String> map = new HashMap<>();
        //判断差出来的admin 为null 返回false 不为空返回true
        if (admin!=null){
            map.put("token", adminDTO.getUsername());
            return toDataResult(map);
        }

     return toDataResult(false);

    }

    @ApiOperation("拿到token,暂时是用户名")
    @GetMapping("/info")
    public Result getInfo(String token) {
        log.debug("进入了info方法,token:{}", token);
        //这边拿到的token就是用户名,需要响应的数据如下,roles权限数组，name用户名,avatar头像
        List<String> roles = new ArrayList<>();

        HashMap<String, Object> map = new HashMap<>();
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUserName, token);
        Admin admin = adminService.getOne(queryWrapper);
        //如果查出来的isAdmin等于1 那么他就是管理员它有管理员该有的权限
        if (admin.getIsAdmin()==1){
            roles.add("admin");
            roles.add("user");
            roles.add("dududu");
        }else {   //不等于那就是普通用户 更普通用户的权限进行绑定
            roles.add("user");
            roles.add("dudu");
        }

        map.put("name", admin.getUserName());
        map.put("avatar",admin.getAvatar());
        map.put("roles", roles);

        return toDataResult(map);
    }

    @ApiOperation("Logout登出")
    @PostMapping("/logout")
    public Result LogOut() {

        return toDataResult(true);

    }

    @ApiOperation("分页+条件")
    @PostMapping("/page/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable Integer currentPage,
                          @PathVariable Integer pageSize,
                          @RequestBody QueryAdminDTO queryAdminDTO) {
        //1.数据校验
        //2.调用方法，拿到返回值
        PageData pageData = adminService.getPageData(currentPage, pageSize, queryAdminDTO);
        //3.返回数据
        return toDataResult(pageData);

    }



    @ApiOperation("修改和新增")
    @PostMapping("/updateAndAdd")
    public Result updateAndAdd(@RequestBody Admin admin) {

        boolean update = adminService.saveOrUpdate(admin);

        if (update){
            String avatar = admin.getAvatar();
            String filename = avatar.substring(avatar.lastIndexOf("/")+1);
            redisTemplate.opsForSet().add("redis_db_upload_image",filename);
        }


        return toResult(update);
    }

    @ApiOperation("根据id查询")
    @GetMapping("/get/{id}")
    public Result getAdminById(@PathVariable String id) {
        Admin admin = adminService.getById(id);

        return toDataResult(admin);
    }


    @ApiOperation("根据userName查询")
    @GetMapping("/find/{userName}")
    public Result getAdminByName(@PathVariable String userName) {



        QueryWrapper<Admin> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_name",userName);
        Admin admin = adminService.getOne(queryWrapper);

        return toDataResult(admin);
    }

    @ApiOperation("根据id删除")
    @DeleteMapping("/delete/{id}")
    public Result deleteTrainer(@PathVariable String id) {

        boolean b = adminService.removeById(id);

        return toResult(b);
    }

    @ApiOperation("批量删除1")
    @PostMapping("/delete")
    public Result delTrainers(@RequestBody List<String> ids) {


        boolean b = adminService.removeBatchByIds(ids);

        return toResult(b);

    }

    @ApiOperation("查询用户名")
    @GetMapping("/findUserNames")
    public Result findUserNames() {


      List<String> userNames =  adminService.findAllUserName();


        return toDataResult(userNames);

    }



}
